package org.ym.demo.service.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;
import org.ym.base.service.BaseService;
import org.ym.demo.dao.model.Event;

@Component
public class EventServiceImpl extends BaseService {

	/**
	 * 测试一级缓存evit
	 */
	public void getFirstCacheTest() {
		System.out.println("1");
		Event event = (Event) getCurrentSession().get(Event.class, 4L);
		System.out.println("2");
		Event event2 = (Event) getCurrentSession().get(Event.class, 4L);
		getCurrentSession().evict(event);
		System.out.println("3");
		Event event3 = (Event) getCurrentSession().get(Event.class, 4L);
		getCurrentSession().clear();
		System.out.println("4");
		Event event4 = (Event) getCurrentSession().get(Event.class, 4L);

		System.out.println(getCurrentSession().contains(event4));
		System.out.println(event.getTitle());
	}
	
	/**
	 * 测试回滚
	 */
	public void add(){
		Event e1 = new Event();
		e1.setTitle("好3");
		getCurrentSession().save(e1);
		int i = 0;
		i /= i;

		Event e2 = new Event();
		e2.setTitle("好4");
		getCurrentSession().save(e2);
		System.out.println(e1.getId());
		System.out.println(e1.getTitle());
	}
	
	/**
	 * 测试二级缓存
	 */
	public void getSecondCacheTest() {
		Query query = getCurrentSession().createQuery("from Event");
		query.setCacheable(true);
		List list = query.list();
		 list = query.list();
		System.out.println(list);
	}
	
	public void saveone(){
		Event e1 = new Event();
		e1.setTitle("哈哈哈");
		getCurrentSession().save(e1);
	}
	
	public void updatetwo(){
		int i = 0;
//		i/=i;
		Query query = getCurrentSession().createQuery("from Event where title='哈哈哈'");
		Event e = (Event) query.list().get(0);
		e.setTitle("哈哈哈修改");
		getCurrentSession().save(e);
	}
	
	/**
	 * 测试回滚
	 */
	public void updatethree(){
		saveone();
		updatetwo();
	}

	public List get() {
		Query query = getCurrentSession().createQuery("from Event where id = '4'");
		List list = query.list();
		list = query.list();
		for (Object o : list) {
			Event e = (Event) o;
			System.out.println(e.getTitle());
		}
		return list;
	}
}
